from typing import Dict, Any
from langchain_openai import ChatOpenAI
from src.agents.base import BaseAgent
from src.core.state import AgentState
from config import config
import logging

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class AgentRouter(BaseAgent):
    """
    Router agent that determines which specialized agent should handle a request.
    """
    
    def __init__(self):
        self.llm = ChatOpenAI(
            model=config.llm_model,
            openai_api_key=config.llm_api_key,
            openai_api_base=config.llm_api_base,
            temperature=0
        )
        logger.info(f"AgentRouter initialized with model: {config.llm_model}")
        
    def process(self, state: AgentState) -> Dict[str, Any]:
        """
        Determine which agent should handle the request.
        """
        messages = state["messages"]
        user_input = messages[-1].content if messages else ""
        
        logger.info(f"Router processing input: {user_input}")
        
        # Routing logic
        user_input_lower = user_input.lower()
        if "jira" in user_input_lower or "issue" in user_input_lower:
            agent_type = "jira"
            logger.info("Routing to Jira agent based on keyword matching")
        elif "conference" in user_input_lower or "meeting" in user_input_lower or "event" in user_input_lower:
            agent_type = "conference"
            logger.info("Routing to Conference agent based on keyword matching")
        else:
            agent_type = "default"
            logger.info("No specific agent matched, using default routing")
            
        return {
            "current_agent": agent_type
        }
    
    def route(self, state: AgentState) -> str:
        """
        Route to the appropriate next node.
        """
        agent = state.get("current_agent", "default")
        logger.info(f"Routing decision: {agent}")
        
        if agent == "jira":
            return "jira_agent"
        elif agent == "conference":
            return "conference_agent"
        else:
            return "end"